<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.defaultNumber"
          clearable
          placeholder="订单编号"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.operPersonName"
          clearable
          placeholder="操作員工"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.inType"
          clearable
          placeholder="类型"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.audit"
          clearable
          placeholder="审核状态"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <date-range-picker v-model="modelCreatedAt" class="date-item" :fix-date.sync="query.signDate" />
        <!-- 搜索 -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <template slot="left">
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-plus"
            @click="creatInventoryLossInBill"
          >
            新增盘亏入库单
          </el-button>
        </template>
        <template slot="left">
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-plus"
            @click="creatReturnInBill"
          >
            新增退货入库单
          </el-button>
        </template>
        <template slot="right">
          <el-button
            class="filter-item"
            size="mini"
            type="success"
          >
            审核
          </el-button>
        </template>
      </crudOperation>
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%" stripe @selection-change="crud.selectionChangeHandler">
        <el-table-column
          type="selection"
          :row-key="getRowKeys"
        />
        <el-table-column
          prop="depot.name"
          label="仓库"
        />
        <el-table-column
          prop="sourceNo"
          label="源单号"
        />
        <el-table-column
          prop="defaultNumber"
          label="单据号"
        />
        <el-table-column
          prop="inType"
          label="入库类型"
        >
          <template slot-scope="scope">
            {{ dict.label.inBillType[scope.row.inType] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="sumQuantity"
          width="120"
          label="入库总数量	"
        />
        <el-table-column
          prop="sumSurplusQuantity"
          width="120"
          label="入库总剩余数量"
        />
        <el-table-column
          prop="createdAt"
          label="创建时间"
        />
        <el-table-column
          prop="inTime"
          label="入库时间"
        />
        <el-table-column
          prop="operPersonName"
          label="操作員工"
        />
        <el-table-column
          align="right"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
            />
            <el-button
              size="mini"
              type="success"
              @click="showDetails(scope.row)"
            >详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDepot from '@/api/wms/depotBillIn'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import DateRangePicker from '@/components/DateRangePicker'
const defaultForm = { name: null }
export default {
  name: 'InBill',
  components: { pagination, rrOperation, crudOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['inBillType'],
  cruds() {
    return CRUD({ title: '出库单', url: 'depotBillIn/page', sort: 'id:desc',
      crudMethod: { ...crudDepot }, optShow: {
        add: false,
        edit: false,
        del: true,
        download: false,
        reset: false
      }})
  },
  data() {
    return {
      modelCreatedAt: '', // 时间选择器显示时间
      ids: [],
      permission: {
        add: ['admin', 'product:add'],
        edit: ['admin', 'product:edit'],
        del: ['admin', 'product:del']
      },
      selectedData: [],
      productId: [],
      queryTypeOptions: [
        { key: 'nameCn', display_name: '中文商品名称' }
      ],
      dialogVisible: false,
      tableData: []
    }
  },
  methods: {
    getModifyKey() {
      if (this.ids.length > 1) {
        this.$message.error('不能同时修改多条数据')
        return false
      }
      if (this.ids.length === 0) {
        this.$message.error('请选择修改的采购单')
        return false
      }
      return true
    },
    getRowKeys(row) {
      this.ids.push(row.id)
      return row.id
    },
    handleEdit(row) {
      const purchaseId = row.id
      if (row.inType === 1) {
        console.log(row)
        this.$router.push({ path: '/wms/creatReturnInBill', name: 'CreatReturnInBill', query: { purchaseId: purchaseId }, params: { purchaseItem: row }})
      } else if (row.inType === 3) {
        console.log(row)
        this.$router.push({ path: '/wms/creatInventoryLossInBill', name: 'CreatInventoryLossInBill', query: { purchaseId: purchaseId }, params: { purchaseItem: row }})
      }
    },
    createInBill() {
      this.$router.push({ path: '/wms/creatInBill' })
    },
    creatInventoryLossInBill() {
      this.$router.push({ path: '/wms/creatInventoryLossInBill' })
    },
    creatReturnInBill() {
      this.$router.push({ path: '/wms/creatReturnInBill' })
    },
    showDetails(row) {
      const purchaseId = row.id
      this.$router.push({ path: '/wms/inBillDetails', name: 'InBillDetails', query: { purchaseId: purchaseId }, params: { purchaseItem: row }})
    }
  }
}
</script>

<style scoped>
.crud-opts {
  padding: 4px 0;
  display: -webkit-flex;
  display: flex;
  align-items: center;
}
.crud-opts .crud-opts-right {
  margin-left: auto;
}
</style>
